
var initScreen = function(callback){//初始化html  font-size
    //$("html").css("font-size",document.documentElement.clientHeight/document.documentElement.clientWidth<1.5 ? (document.documentElement.clientHeight/603*312.5+"%") : (document.documentElement.clientWidth/375*312.5+"%")); //单屏全屏布局时使用,短屏下自动缩放
    $("html").css("font-size",document.documentElement.clientWidth/375*312.5+"%");//长页面时使用,不缩放
    if(callback)callback();
},
init = function(){
    var _data = "";
    navTab = function(){
        //导航
        var nav_items_li = $(".nav-items li");
        var _this;
        /*var nav_items_li_length = nav_items_li.length;
        var nav_items_width = 0;
        for(var i = 0; i < nav_items_li_length; i++){
            nav_items_width += nav_items_li.eq(i).width();
            console.log(nav_items_li.eq(i).width());
        }
        $(".nav-items").css("width",nav_items_width);*/
        nav_items_li.on('mousedown',function(e){
            _this = $(this).index();
            nav_items_li.removeClass('cur').eq(_this).addClass('cur');
        });
        

    },
    _addEvent = function(){
        window.addEventListener("orientationchange" in window ? "orientationchange" : "resize",function(e){
            var st=setTimeout(initScreen,300);
        }, false);
    },
    getData = function(callback){
        $.ajax({
            url : "http://m.aipai.com/aipaiApi/indexPage/allGameData?func=zone&module=indexPage&gameId=1106&gameType=2&os=3&aipaiAppNativeSetCookies=no",
            type: "post",
            dataType: "jsonp",
            async: false,
            success: function(data){
                //console.log(data);
                _data = data;
                //data = JSON.parse(data);
                /*var html = "";
                for( var i = 0; i < _data.data.tabList.length; i++){
                    html += '<li>\
                                <a gameId="'+ _data.data.tabList[i].gameId +'" href="'+ _data.data.tabList[i].url +'">'+ _data.data.tabList[i].gameName +'</a>\
                            </li>';
                };
                $(".nav-items").append(html);*/
                //console.log(_data)
                if(callback){callback()};
            }
        })
    },
    renderTab = function(){
        var html = "";
        for( var i = 0; i < _data.data.tabList.length; i++){
            html += '<li>\
                        <a gameId="'+ _data.data.tabList[i].gameId +'" href="'+ _data.data.tabList[i].url +'">'+ _data.data.tabList[i].gameName +'</a>\
                    </li>';
        };
        //console.log(_data)
        $(".nav-items").append(html);
        
    },
    renderBanner = function(){
        var html = "";
        for( var i = 0; i < _data.data.focusPicList.length; i++){
            html += '<div class="swiper-slide">\
                        <a href="'+ _data.data.focusPicList[i].openValue.url +'">\
                            <img src="'+ _data.data.focusPicList[i].img +'" alt="">\
                        </a>\
                    </div>';
        };
        $(".swiper-wrapper").append(html);
        //轮播图
        var mySwiperH = new Swiper ('.swiper-container-h', {
            direction: 'horizontal',
            loop: true,
            observer:true,
            observeParents:true,
            pagination: '.swiper-pagination-h',
            autoplay : 5000,
            autoplayDisableOnInteraction: false
        });
        
    },
    renderRecommend = function(){
        var index;
        var length = _data.data.videoList.length;
        $(".recommend .hd-r").on("touchstart",function(){
            index = $(".recommend .bd ul li").last().attr("index");
            console.log(index);
            if( index == 3){
                load(4,8);
            }else{
                load(0,4);
            }
        })
        function load(s,e){
            var html = "";
            for( var i = s; i < e; i++){
                html += '<li index="'+ i +'">\
                            <div class="pic">\
                                <a href="'+ _data.data.videoList[i].videoEntity.url +'">\
                                    <img src="'+ _data.data.videoList[i].videoEntity.videoPic +'" alt="">\
                                </a>\
                                <div class="user-photo">\
                                    <a href="'+ _data.data.videoList[i].userEntity.homeUrl +'">\
                                        <img src="'+ _data.data.videoList[i].userEntity.userPic +'" alt="">\
                                    </a>\
                                </div>\
                                <div class="user-info">\
                                    <div class="user-name">'+ _data.data.videoList[i].userEntity.nickName +'</div>\
                                    <div class="view">'+ _data.data.videoList[i].videoEntity.playCount +'</div>\
                                </div>\
                            </div>\
                            <div class="description">\
                                <a href="'+ _data.data.videoList[i].videoEntity.url +'">'+ _data.data.videoList[i].videoEntity.title +'</a>\
                            </div>\
                        </li>';
            };
            //console.log(html);
            $(".recommend .bd ul").html(html);
        };
        load(0,4);
        
    },
    scrollLoad = function(ele,sec){
        var $win = $(window);
        $win.scroll(function(){
            var $ele = $(ele),
                scroll = $win.scrollTop() + $win.height(),
                subScroll = $win.scrollTop() + $win.height(),
                itemTop = $ele.offset().top + $ele.height(),
                num = $(".box-n2 .tab-hd").data("index");
                //图片延迟加载
                $("img").each(function(i){
                    var imgTop = $("img").eq(i).offset().top;
                    if(subScroll >= imgTop){
                        if($("img").eq(i).data("src") != undefined){
                            $("img").eq(i).attr({"src":$(this).data("src")});
                            console.log($("img").eq(i).data("src"))
                        }
                    }
                });
                if(parseInt(scroll) >= parseInt(itemTop)){
                    if($ele.hasClass("load")){
                        return false;
                    }else{
                        $ele.addClass("load");
                        $ele.append("<p class='more'>正在加载...</p>");
                        var tipsH = $(".more").height(),
                            index = Number($ele.find(sec).last().attr("index"));
                        
                        if(num == 1){
                            $.ajax({
                                type: "POST",
                                url: "http://m.aipai.com/mobile/apps/apps.php?module=gameIndex&func=dingData&from=h5&spread=0&gameId=1106&livestate=0&os=1&page=1&pageSize=20",
                                dataType: "jsonp",
                                async: true,
                                data: "",
                                success: function (data) {
                                    var html = "";
                                    var maxIndex = index+5;
                                    //var copyMaxIndex;
                                    if(index+1 >= data.data.length){
                                        $ele.find(".more").text("没有内容了...");
                                        return false;
                                    }else{
                                        //copyMaxIndex = index+5;
                                        if(maxIndex >= data.data.length){
                                            maxIndex = data.data.length
                                        }
                                        for(var i = index+1; i< maxIndex; i++){
                                            html += '<section class="js-box video-items" index="'+ i +'">\
                                                        <dl class="video-item flex-cont">\
                                                            <dt>\
                                                                <a href="'+ data.data[i].userInfo.homeUrl +'"><img data-src="'+ data.data[i].userInfo.userPic +'" src="" alt=""></a>\
                                                            </dt>\
                                                            <dd class="flex">\
                                                                <div class="hd wrapfix">\
                                                                    <div class="hd-l user-name vip">'+ data.data[i].userInfo.nickname +'</div>\
                                                                    <div class="hd-r concern">粉他</div>\
                                                                </div>\
                                                                <div class="bd">\
                                                                    <p class="description">\
                                                                        <a href="'+ data.data[i].gameUrl +'">'+ data.data[i].detail +'</a>\
                                                                    </p>\
                                                                    <div class="video">\
                                                                        <a href="'+ data.data[i].gameUrl +'">\
                                                                            <img data-src="'+ data.data[i].thumb +'" class="pic" src="" alt="">\
                                                                            <img class="play" src="images/play_bb5e27c.png" alt="">\
                                                                            <div class="view">'+ data.data[i].click +'</div>\
                                                                        </a>\
                                                                    </div>\
                                                                </div>\
                                                            </dd>\
                                                        </dl>\
                                                    </section>'
                                        };
                                        $ele.children().append(html);
                                        $ele.removeClass("load");
                                        $ele.find(".more").remove();
                                        // loding
                                        $(".con_load").hide();
                                        $(".wrap-main").removeClass("hidden");
                                    }
                                }
                            })
                        }else if(num == 2){
                            $.ajax({
                                type: "POST",
                                url: "http://m.aipai.com/mobile/apps/apps_module-gameIndex_func-newAsset_sort-click_gameId-1106_page-1_pageSize-12.html",
                                async: true,
                                dataType: "jsonp",
                                data: "",
                                success: function (data) {
                                    var html = "";
                                    var maxIndex = index+5;
                                    //var copyMaxIndex;
                                    if(index+1 >= data.data.length){
                                        $ele.find(".more").text("没有内容了...");
                                        return false;
                                    }else{
                                        //copyMaxIndex = index+5;
                                        if(maxIndex >= data.data.length){
                                            maxIndex = data.data.length
                                        }
                                        for(var i = index+1; i< maxIndex; i++){
                                            html+= '<li class="flex-cont js-box" index="'+ i +'">\
                                                        <a href="'+ data.data[i].url +'" class="pic">\
                                                            <img data-src="'+ data.data[i].thumbFileName +'" src="" alt="">\
                                                        </a>\
                                                        <div class="text flex">\
                                                            <p class="tit">\
                                                                <a href="'+ data.data[i].url +'">'+ data.data[i].title +'</a>\
                                                            </p>\
                                                            <p class="user-name">\
                                                                <a href="'+ data.data[i].homeUrl +'">'+ data.data[i].nickname +'</a>\
                                                            </p>\
                                                            <p class="view">'+ data.data[i].click +'</p>\
                                                        </div>\
                                                        <span class="num">'+ Number(i+1) +'</span>\
                                                    </li>';
                                        };
                                        $ele.children().append(html);
                                        $ele.removeClass("load");
                                        $ele.find(".more").remove();
                                    }
                                },
                                error: function(){
                                    
                                }
                            });
                        }else if(num == 3){
                            $.ajax({
                                type: "POST",
                                url: "http://m.aipai.com/mobile/apps/apps.php?module=gameIndex&func=star&gameId=1106&page=1&pageSize=12&userId=11935881&aipaiAppNativeSetCookies=no",
                                async: true,
                                dataType: "jsonp",
                                data: "",
                                success: function (data) {
                                    //console.log(data);
                                    var html = "";
                                    var maxIndex = index+5;
                                    //var copyMaxIndex;
                                    if(index+1 >= data.data.length){
                                        $ele.find(".more").text("没有内容了...");
                                        return false;
                                    }else{
                                        //copyMaxIndex = index+5;
                                        if(maxIndex >= data.data.length){
                                            maxIndex = data.data.length
                                        }
                                        for(var i = index+1; i< maxIndex; i++){
                                            html+= '<li class="js-box flex-cont flex-centerbox-y" index="'+ i +'">\
                                                    <a href="'+ data.data[i].homeUrl +'" class="pic">\
                                                        <img data-src="'+ data.data[i].normal +'" src="" alt="">\
                                                    </a>\
                                                    <div class="text flex">\
                                                        <p class="user-name">\
                                                            <a href="'+ data.data[i].homeUrl +'">'+ data.data[i].nickname +'</a>\
                                                        </p>\
                                                        <p class="fs">\
                                                            <span>'+ data.data[i].fansCount +'</span>&nbsp;粉丝\
                                                        </p>\
                                                        <p class="desc">'+ data.data[i].des +'</p>\
                                                    </div>\
                                                    <div class="star">\
                                                        <p>粉</p>\
                                                    </div>\
                                                </li>';
                                        };
                                        $ele.children().append(html);
                                        $ele.removeClass("load");
                                        $ele.find(".more").remove();
                                    }
                                },
                                error: function(){
                                }
                            });
                        }else if(num == 4){
                        
                            $.ajax({
                                type: "POST",
                                url: "http://m.aipai.com/mobile/apps/apps_module-gameIndex_func-subjectList_gameId-1106_page-1_pageSize-12.html",
                                async: true,
                                dataType: "jsonp",
                                data: "",
                                success: function (data) {
                                    console.log(data);
                                    var html = "";
                                    var maxIndex = index+5;
                                    //var copyMaxIndex;
                                    if(index+1 >= data.data.length){
                                        $ele.find(".more").text("没有内容了...");
                                        return false;
                                    }else{
                                        //copyMaxIndex = index+5;
                                        if(maxIndex >= data.data.length){
                                            maxIndex = data.data.length
                                        }
                                        for(var i = index+1; i< maxIndex; i++){
                                            html += '<li class="js-box" index="'+ i +'">\
                                                        <a href="'+ data.data[i].url +'">\
                                                            <img data-src="'+ data.data[i].cover +'" src="" alt="">\
                                                            <p class="desc">'+ data.data[i].title +'</p>\
                                                        </a>\
                                                    </li>';
                                        };
                                        $ele.children().append(html);
                                        $ele.removeClass("load");
                                        $ele.find(".more").remove();
                                    }
                                },
                                error: function(){
                                }
                            });
                        }   
                    }
                }

        })
    },
    dataBtn = function(){
        //tab
        var tab_hd_ul_li = $(".tab-hd ul li");
        var tab_bd = $(".tab-bd");
        var _this;
        tab_hd_ul_li.on('touchstart mousedown',function(e){
            _this = $(this).index();
            tab_hd_ul_li.removeClass('cur').eq(_this).addClass('cur');
            tab_bd.removeClass("show").eq(_this).addClass("show");
            $(".box-n2 .tab-hd").data("index",_this+1);
        });
    },
    // 顶顶数据初始化
    dingData = function () {
        $.ajax({
            type: "POST",
            url: "http://m.aipai.com/mobile/apps/apps.php?module=gameIndex&func=dingData&from=h5&spread=0&gameId=1106&livestate=0&os=1&page=1&pageSize=20",
            dataType: "jsonp",
            async: true,
            data: "",
            success: function (data) {
                console.log(data);
                var html = "";
                for(var i = 0; i< 4; i++){
                    html += '<section class="js-box video-items" index="'+ i +'">\
                                <dl class="video-item flex-cont">\
                                    <dt>\
                                        <a href="'+ data.data[i].userInfo.homeUrl +'"><img data-src="'+ data.data[i].userInfo.userPic +'" src="" alt=""></a>\
                                    </dt>\
                                    <dd class="flex">\
                                        <div class="hd wrapfix">\
                                            <div class="hd-l user-name vip">'+ data.data[i].userInfo.nickname +'</div>\
                                            <div class="hd-r concern">粉他</div>\
                                        </div>\
                                        <div class="bd">\
                                            <p class="description">\
                                                <a href="'+ data.data[i].gameUrl +'">'+ data.data[i].detail +'</a>\
                                            </p>\
                                            <div class="video">\
                                                <a href="'+ data.data[i].gameUrl +'">\
                                                    <img class="pic" src="'+ data.data[i].thumb +'" alt="">\
                                                    <img class="play" src="images/play_bb5e27c.png" alt="">\
                                                    <div class="view">'+ data.data[i].click +'</div>\
                                                </a>\
                                            </div>\
                                        </div>\
                                    </dd>\
                                </dl>\
                            </section>'
                };
                $(".video-list").append(html);
                // loding
                $(".con_load").hide();
                $(".wrap-main").removeClass("hidden");
            },
            error: function(){
                
            }
        });
    },
    // 榜单顶数据初始化
    bangDate = function () {
        $.ajax({
            type: "POST",
            url: "http://m.aipai.com/mobile/apps/apps_module-gameIndex_func-newAsset_sort-click_gameId-1106_page-1_pageSize-12.html",
            async: true,
            dataType: "jsonp",
            data: "",
            success: function (data) {
                //var data = JSON.parse(data);
                //console.log(data);
                var html = "";
                for(var i = 0; i< 4; i++){
                    html+= '<li class="flex-cont js-box" index="'+ i +'">\
                                <a href="'+ data.data[i].url +'" class="pic">\
                                    <img src="'+ data.data[i].thumbFileName +'" alt="">\
                                </a>\
                                <div class="text flex">\
                                    <p class="tit">\
                                        <a href="'+ data.data[i].url +'">'+ data.data[i].title +'</a>\
                                    </p>\
                                    <p class="user-name">\
                                        <a href="'+ data.data[i].homeUrl +'">'+ data.data[i].nickname +'</a>\
                                    </p>\
                                    <p class="view">'+ data.data[i].click +'</p>\
                                </div>\
                                <span class="num">'+ Number(i+1) +'</span>\
                            </li>';
                };
                $(".hot-list").append(html);
            },
            error: function(){
                
            }
        });
    },
    // 大神数据初始化
    dsDate = function () {
        $.ajax({
            type: "POST",
            url: "http://m.aipai.com/mobile/apps/apps.php?module=gameIndex&func=star&gameId=1106&page=1&pageSize=12&userId=11935881&aipaiAppNativeSetCookies=no",
            async: true,
            dataType: "jsonp",
            data: "",
            success: function (data) {
                //console.log(data);
                var html = "";
                for(var i = 0; i < 4; i++){
                    html+= '<li class="js-box flex-cont flex-centerbox-y" index="'+ i +'">\
                            <a href="'+ data.data[i].homeUrl +'" class="pic">\
                                <img src="'+ data.data[i].normal +'" alt="">\
                            </a>\
                            <div class="text flex">\
                                <p class="user-name">\
                                    <a href="'+ data.data[i].homeUrl +'">'+ data.data[i].nickname +'</a>\
                                </p>\
                                <p class="fs">\
                                    <span>'+ data.data[i].fansCount +'</span>&nbsp;粉丝\
                                </p>\
                                <p class="desc">'+ data.data[i].des +'</p>\
                            </div>\
                            <div class="star">\
                                <p>粉</p>\
                            </div>\
                        </li>';
                };
                $(".Okami-list").append(html);
            },
            error: function(){
            }
        });
    },
    // 专题数据初始化
    ztDate = function () {
        $.ajax({
            type: "POST",
            url: "http://m.aipai.com/mobile/apps/apps_module-gameIndex_func-subjectList_gameId-1106_page-1_pageSize-12.html",
            async: true,
            dataType: "jsonp",
            data: "",
            success: function (data) {
                //console.log(data);
                var html = "";
                for(var i = 0; i < 4; i++){
                    html += '<li class="js-box" index="'+ i +'">\
                                <a href="'+ data.data[i].url +'">\
                                    <img src="'+ data.data[i].cover +'" alt="">\
                                    <p class="desc">'+ data.data[i].title +'</p>\
                                </a>\
                            </li>';
                };
                $(".features-list").append(html);
            },
            error: function(){
            }
        });
    };
    navTab();
    _addEvent();
    getData(function(){
         renderTab();
         renderBanner();
         renderRecommend();
    });
    scrollLoad(".box-n2 .show",".js-box");
    dataBtn();
    dingData();
    bangDate();
    dsDate();
    ztDate();
}
$(function(){
    initScreen(function(){
        init();
        /*navTab();
        _addEvent();
        getaData();*/
        }
    );
})